package com.app.business.utils;

import com.app.business.model.Contract;
import com.app.business.model.House;
import com.app.business.service.ContractService;
import com.app.business.service.HouseService;
import com.app.system.utils.ApplicationContextProvider;
import com.app.system.utils.CollectionUtils;
import com.app.system.utils.dataType.BigDecimalUtils;
import com.app.system.utils.dataType.StringUtils;
import com.google.common.collect.Maps;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;

import java.math.BigDecimal;
import java.util.*;

/**
 * 对不同的选房方案进行限制选房
 * Created by ddz on 2022/3/16.
 */
@SuppressWarnings("all")
public class ChooseLimitUtils {
    private static HouseService houseService;

    public static String getChooseLimit(Contract contract, House house) {
        ApplicationContext applicationContext = ApplicationContextProvider.getApplicationContext();
        houseService = (HouseService) applicationContext.getBean(HouseService.class);
        String chooseLimitMemo = "";
        Integer limitType = contract.getChooseLimitType();
        List<House> chooseHouseList = houseService.getHouseListByContractIdForBuildArea(contract.getId());
        chooseHouseList.add(house);
        //获取面积最小的house
        House minAreaHouse = chooseHouseList.stream().min(Comparator.comparing(House::getBuildArea)).get();
        if (limitType != null && limitType == 1) {
            chooseLimitMemo = "";
        } else if (limitType != null && limitType == 2) {
            chooseLimitMemo = "";
        } else if (limitType != null && limitType == 3) {
            //判断是否有选过保障区房源
            for (House house1 : chooseHouseList) {
                String memo = house1.getMemo();
                if (house.getMemo() != null && house.getMemo().equals("保障区房源")) {
                    if (memo.equals("保障区房源")) {
                        chooseLimitMemo = "该户不可选择该房，已选择一套保障区房源;";
                        break;
                    }
                }
            }
            //判断是否一号单选
            if (chooseHouseList.size() <= 1) {
                if (contract.getLimitHouseAreaTotal().compareTo(house.getBuildArea()) >= 0) {
                    chooseLimitMemo = "";
                } else {
                    chooseLimitMemo = "当前可选房源套型面积有：";
                    List<BigDecimal> buildAreaIntList = houseService.getBuildAreaIntListByHouseId(contract, "", contract.getLimitHouseAreaTotal());
                    int num = 0;
                    for (BigDecimal bigDecimal : buildAreaIntList) {
                        if (num < 3) {
                            chooseLimitMemo = chooseLimitMemo + String.valueOf(bigDecimal) + "㎡,";
                        }
                        num = num + 1;
                    }
                    if (chooseLimitMemo.contains((house.getBuildArea().setScale(0, BigDecimal.ROUND_DOWN).toString()))) {
                        chooseLimitMemo = chooseLimitMemo + "当前套型符合选房规则!";
                    } else {
                        chooseLimitMemo = chooseLimitMemo + "该户不可选择该房!";
                    }
                }
            } else if (chooseHouseList.size() > 1) {
                //不是一号单选
                BigDecimal allChooseArea = new BigDecimal("0");
                for (House house2 : chooseHouseList) {
                    //当前所有已选房面积合计
                    allChooseArea = BigDecimalUtils.getSum(allChooseArea, house2.getBuildArea());
                }
                if (BigDecimalUtils.getBigAndBigSub(allChooseArea, minAreaHouse.getBuildArea()).compareTo(contract.getLimitHouseAreaTotal()) > 0) {
                    //如果减去当前最小房源面积仍已扩购
                }
                //如果总可安置面积大于等于总已选面积（包含当前已选未保存房源），则不限制
                if (contract.getLimitHouseAreaTotal().compareTo(allChooseArea) >= 0) {
                    chooseLimitMemo = "";
                } else {
                    //不计算所选最小户型，所剩余面积
                    BigDecimal syArea = BigDecimalUtils.getBigAndBigSub(contract.getLimitHouseAreaTotal(),
                            BigDecimalUtils.getBigAndBigSub(allChooseArea, minAreaHouse.getBuildArea()));
                    if (syArea.compareTo(new BigDecimal("40")) < 0) {

                    }
                    chooseLimitMemo = "根据当前所选套型，最小可选房源套型面积有：";
                    //总安置面积-（总已选面积-最小面积户型面积）
                    List<BigDecimal> buildAreaIntList = houseService.getBuildAreaIntListByHouseId(contract, "", syArea);
                    int num = 0;
                    //二选一
                    for (BigDecimal bigDecimal : buildAreaIntList) {
                        if (num < 2) {
                            chooseLimitMemo = chooseLimitMemo + String.valueOf(bigDecimal) + "㎡,";
                        }
                        num = num + 1;
                    }
                    if (chooseLimitMemo.contains((house.getBuildArea().setScale(0, BigDecimal.ROUND_DOWN).toString()))) {
                        chooseLimitMemo = chooseLimitMemo + "当前套型符合选房规则!";
                    } else {
                        chooseLimitMemo = chooseLimitMemo + "该户不可选择该房!";
                    }
                }
            }
        }
        return chooseLimitMemo;
    }

    public static String getChooseLimitForHosuePic(Contract contract) {
        ApplicationContext applicationContext = ApplicationContextProvider.getApplicationContext();
        houseService = (HouseService) applicationContext.getBean(HouseService.class);
        String chooseLimitMemo = "";
        Integer limitType = contract.getChooseLimitType();
        List<House> chooseHouseList = houseService.getHouseListByContractIdForBuildArea(contract.getId());
        //获取面积最小的house
        if (limitType != null && limitType == 1) {
            chooseLimitMemo = "";
        } else if (limitType != null && limitType == 2) {
            chooseLimitMemo = "";
        } else if (limitType != null && limitType == 3) {
            //判断是否有选过保障区房源
            for (House house : chooseHouseList) {
                if (house.getHouseStyle().equals("保障区房源")) {
                    chooseLimitMemo = "该户已选择一套保障区房源!";
                    break;
                }
            }
//            //判断是否一号单选
//            if (chooseHouseList.size() <= 1) {
//                if (contract.getLimitHouseAreaTotal().compareTo(house.getBuildArea()) >= 0) {
//                    chooseLimitMemo = "";
//                } else {
//                    chooseLimitMemo = "当前可选房源套型面积有：";
//                    List<BigDecimal> buildAreaIntList = houseService.getBuildAreaIntListByHouseId(contract, "", contract.getLimitHouseAreaTotal());
//                    int num = 0;
//                    for (BigDecimal bigDecimal : buildAreaIntList) {
//                        if (num < 3) {
//                            chooseLimitMemo = chooseLimitMemo + String.valueOf(bigDecimal) + "㎡,";
//                        }
//                        num = num + 1;
//                    }
//                    if (chooseLimitMemo.contains((house.getBuildArea().setScale(0, BigDecimal.ROUND_DOWN).toString()))) {
//                        chooseLimitMemo = chooseLimitMemo + "当前套型符合选房规则!";
//                    } else {
//                        chooseLimitMemo = chooseLimitMemo + "该户不可选择该房!";
//                    }
//                }
//            } else if (chooseHouseList.size() > 1) {
//                //不是一号单选
//                BigDecimal allChooseArea = new BigDecimal("0");
//                for (House house2 : chooseHouseList) {
//                    //当前所有已选房面积合计
//                    allChooseArea = BigDecimalUtils.getSum(allChooseArea, house2.getBuildArea());
//                }
//                if (BigDecimalUtils.getBigAndBigSub(allChooseArea, minAreaHouse.getBuildArea()).compareTo(contract.getLimitHouseAreaTotal()) > 0) {
//                    //如果减去当前最小房源面积仍已扩购
//                }
//                //如果总可安置面积大于等于总已选面积（包含当前已选未保存房源），则不限制
//                if (contract.getLimitHouseAreaTotal().compareTo(allChooseArea) >= 0) {
//                    chooseLimitMemo = "";
//                } else {
//                    //不计算所选最小户型，所剩余面积
//                    BigDecimal syArea = BigDecimalUtils.getBigAndBigSub(contract.getLimitHouseAreaTotal(),
//                            BigDecimalUtils.getBigAndBigSub(allChooseArea, minAreaHouse.getBuildArea()));
//                    if (syArea.compareTo(new BigDecimal("40")) < 0) {
//
//                    }
//                    chooseLimitMemo = "根据当前所选套型，最小可选房源套型面积有：";
//                    //总安置面积-（总已选面积-最小面积户型面积）
//                    List<BigDecimal> buildAreaIntList = houseService.getBuildAreaIntListByHouseId(contract, "", syArea);
//                    int num = 0;
//                    //二选一
//                    for (BigDecimal bigDecimal : buildAreaIntList) {
//                        if (num < 2) {
//                            chooseLimitMemo = chooseLimitMemo + String.valueOf(bigDecimal) + "㎡,";
//                        }
//                        num = num + 1;
//                    }
//                    if (chooseLimitMemo.contains((house.getBuildArea().setScale(0, BigDecimal.ROUND_DOWN).toString()))) {
//                        chooseLimitMemo = chooseLimitMemo + "当前套型符合选房规则!";
//                    } else {
//                        chooseLimitMemo = chooseLimitMemo + "该户不可选择该房!";
//                    }
//                }
//            }
        }
        return chooseLimitMemo;
    }


    //获取某户当前不可选房源IdList
    public static List<Integer> getCountChooseHouseIdList(Contract contract) {
        List<Integer> houseIdList = new ArrayList<>();
        ApplicationContext applicationContext = ApplicationContextProvider.getApplicationContext();
        houseService = (HouseService) applicationContext.getBean(HouseService.class);
        String chooseLimitMemo = null;
        Integer limitType = contract.getChooseLimitType();
        List<House> chooseHouseList = houseService.getHouseListByContractIdForBuildArea(contract.getId());
        Map<String, Integer> houseHoldCountMap = houseService.getHouseMapByHouseHoldAndZsprojectId(contract.getZsProject().getId());
        BigDecimal minArea = null;
        if (chooseHouseList.size() > 0) {
            //获取面积最小的house
            House minAreaHouse = chooseHouseList.stream().min(Comparator.comparing(House::getBuildArea)).get();
            //获取已选房最小面积
            minArea = minAreaHouse.getBuildArea();
        } else {
            minArea = new BigDecimal("0");
        }
        if (limitType != null && limitType == 1) {
            houseIdList = null;
        } else if (limitType != null && limitType == 2) {
            houseIdList = null;
        } else if (limitType != null && limitType == 3) {
            //判断是否有选过保障区房源
            for (House house : chooseHouseList) {
                if (house.getHouseStyle() != null && house.getHouseStyle().equals("保障区房源")) {
                    chooseLimitMemo = "已选择保障区房源";
                }
            }
            //判断是否有选房，如果未选房，则不大于总安置面积，且升档三个套型
            if (chooseHouseList.size() == 0) {
                String houseHoldStr = "";
                if (contract.getLimitHouseAreaTotal().compareTo(BigDecimal.ZERO) > 0
                        && contract.getLimitHouseAreaTotal().compareTo(new BigDecimal("40")) < 0) {
                    //如果面积小于40，则不允许选房
                    houseIdList = houseService.getHouseIdListByZsProjectIdAndHouseStyle(contract.getZsProject().getId(), chooseLimitMemo, new BigDecimal("0"));
                    return houseIdList;
                } else if (contract.getLimitHouseAreaTotal().compareTo(new BigDecimal("40")) >= 0
                        && contract.getLimitHouseAreaTotal().compareTo(new BigDecimal("90")) < 0) {
                    //如果面积大于等于40，小于90，则,允许选择二房二厅
                    houseHoldStr = "'0202'";
                    if (houseHoldCountMap.get("0202") == null || houseHoldCountMap.get("0202") == 0) {
                        //如果二房二厅房源没了，则,允许选择三房二厅，五房二厅,复式
                        houseHoldStr = "'0302','1000','0502'";
                        if ((houseHoldCountMap.get("0302") == null || houseHoldCountMap.get("0302") == 0)
                                && (houseHoldCountMap.get("1000") == null || houseHoldCountMap.get("1000") == 0)
                                && (houseHoldCountMap.get("0502") == null || houseHoldCountMap.get("0502") == 0)) {
                            //如果三房二厅房源，复式房源没了，则,允许选择四房二厅
                            houseHoldStr = "'0202','0302','0402','1000','0502'";
                        }
                    }
                } else if (contract.getLimitHouseAreaTotal().compareTo(new BigDecimal("90")) >= 0
                        && contract.getLimitHouseAreaTotal().compareTo(new BigDecimal("130")) < 0) {
                    houseHoldStr = "'0202','0302','1000','0502'";
                    //如果面积大于等于90，小于130，则,允许选择二房二厅、三房二厅,五房二厅,复式
                    if ((houseHoldCountMap.get("0202") == null || houseHoldCountMap.get("0202") == 0)
                            && (houseHoldCountMap.get("0302") == null || houseHoldCountMap.get("0302") == 0)
                            && (houseHoldCountMap.get("1000") == null || houseHoldCountMap.get("1000") == 0)
                            && (houseHoldCountMap.get("0502") == null || houseHoldCountMap.get("0502") == 0)) {
                        //如果二房二厅、三房二厅,五房二厅,复式 房源没了，则,允许向上选择四房二厅
                        houseHoldStr = "'0202','0302','0402','1000','0502'";

                    }
                } else if (contract.getLimitHouseAreaTotal().compareTo(new BigDecimal("130")) >= 0) {
                    //如果面积大于等于130，则,允许选择所有户型
                    houseHoldStr = "'0202','0302','0402','1000','0502'";
                }
                List<BigDecimal> buildAreaIntList = houseService.getBuildAreaIntListByHouseId(contract, houseHoldStr, contract.getLimitHouseAreaTotal());
                int num = 1;
                //最大可选面积
                BigDecimal maxArea = contract.getLimitHouseAreaTotal();
                for (BigDecimal bigDecimal : buildAreaIntList) {
                    if (bigDecimal.compareTo(maxArea) > 0) {
                        maxArea = BigDecimalUtils.getSum(bigDecimal, new BigDecimal("0.99"));
                    }
                    //二选一最大可选套型
                    //区间二选一
                    if (contract.getChooseHouseNum().contains("-") && num == 2) {
                        break;
                    } else if (!contract.getChooseHouseNum().contains("-") && num == 3) {
                        //三选一最大可选套型
                        break;
                    }
                    num = num + 1;
                }
                //如果第一套房，最大可选面积为总可安置面积，
                houseIdList = houseService.getHouseIdListByZsProjectIdAndHouseStyle(contract.getZsProject().getId(), chooseLimitMemo, maxArea);
            } else {
                //不是第一套
                //如果已选房面积大于总安置面积
                if (contract.getChooseHouseAreaTotal().compareTo(contract.getLimitHouseAreaTotal()) >= 0) {
                    //已扩购则不允许选房
                    houseIdList = houseService.getHouseIdListByZsProjectIdAndHouseStyle(contract.getZsProject().getId(), chooseLimitMemo, new BigDecimal("0"));
                } else {
                    BigDecimal allChooseArea = BigDecimal.ZERO;
                    BigDecimal syAllChooseArea = BigDecimal.ZERO;
                    for (House house : chooseHouseList) {
                        //当前所有已选房面积合计
                        allChooseArea = BigDecimalUtils.getSum(allChooseArea, house.getBuildArea());
                    }
                    //剩余可选房面积
                    syAllChooseArea = BigDecimalUtils.getBigAndBigSub(contract.getLimitHouseAreaTotal(), allChooseArea);

                    String houseHoldStr = "";
                    if (syAllChooseArea.compareTo(BigDecimal.ZERO) > 0
                            && syAllChooseArea.compareTo(new BigDecimal("40")) < 0) {
                        //如果面积小于40，则不允许选房
                        houseIdList = houseService.getHouseIdListByZsProjectIdAndHouseStyle(contract.getZsProject().getId(), chooseLimitMemo, new BigDecimal("0"));
                        return houseIdList;
                    } else if (syAllChooseArea.compareTo(new BigDecimal("40")) >= 0
                            && syAllChooseArea.compareTo(new BigDecimal("90")) < 0) {
                        //如果面积大于等于40，小于90，则,允许选择二房二厅
                        houseHoldStr = "'0202'";
                        if (houseHoldCountMap.get("0202") == null || houseHoldCountMap.get("0202") == 0) {
                            //如果二房二厅房源没了，则,允许选择三房二厅,五房二厅，复式房源
                            houseHoldStr = "'0202','0302','1000','0502'";
                            if ((houseHoldCountMap.get("0202") == null || houseHoldCountMap.get("0202") == 0)
                                    && (houseHoldCountMap.get("0302") == null || houseHoldCountMap.get("0302") == 0)
                                    && (houseHoldCountMap.get("1000") == null || houseHoldCountMap.get("1000") == 0)
                                    && (houseHoldCountMap.get("0502") == null || houseHoldCountMap.get("0502") == 0)) {
                                //如果三房二厅房源没了，则,允许选择四房二厅，五房二厅，复式房源
                                houseHoldStr = "'0202','0302','0402','1000','0502'";
                            }
                        }
                    } else if (syAllChooseArea.compareTo(new BigDecimal("90")) >= 0
                            && syAllChooseArea.compareTo(new BigDecimal("130")) < 0) {
                        houseHoldStr = "'0202','0302','1000','0502'";
                        //如果面积大于等于90，小于130，则,允许选择二房二厅、三房二厅,五房二厅，复式房源
                        if ((houseHoldCountMap.get("0202") == null || houseHoldCountMap.get("0202") == 0)
                                && (houseHoldCountMap.get("0302") == null || houseHoldCountMap.get("0302") == 0)
                                && (houseHoldCountMap.get("1000") == null || houseHoldCountMap.get("1000") == 0)
                                && (houseHoldCountMap.get("0502") == null || houseHoldCountMap.get("0502") == 0)) {
                            //如果三房二厅房源没了，则,允许向上选择四房二厅，五房二厅，复式房源
                            houseHoldStr = "'0202','0302','0402','1000','0502'";

                        }
                    } else if (syAllChooseArea.compareTo(new BigDecimal("130")) >= 0) {
                        //如果面积大于等于130，则,允许选择所有户型
                        houseHoldStr = "'0202','0302','0402','1000','0502'";
                    }

                    //比剩余安置面积大的套型面积
                    List<BigDecimal> buildAreaIntList = houseService.getBuildAreaIntListByHouseId(contract, houseHoldStr, syAllChooseArea);
                    int num = 1;
                    BigDecimal maxArea = syAllChooseArea;
                    for (BigDecimal bigDecimal : buildAreaIntList) {
                        if (bigDecimal.compareTo(maxArea) > 0) {
                            maxArea = BigDecimalUtils.getSum(bigDecimal, new BigDecimal("0.99"));
                        }
                        //二选一最大可选套型
                        //区间二选一
                        if (num == 2) {
                            break;
                        }
                        num = num + 1;
                    }
                    //可选房源不可超过当前所选最小房面积
                    if (maxArea.compareTo(minArea) > 0) {
                        maxArea = minArea;
                    }
                    houseIdList = houseService.getHouseIdListByZsProjectIdAndHouseStyle(contract.getZsProject().getId(), chooseLimitMemo, maxArea);

                }
            }
        }
        return houseIdList;
    }


    //获取某户当前不可选房源IdList 根据保障区、辅选区不同限定
    public static List<Integer> getCountChooseHouseIdListByHouseStyle(Contract contract, Integer azProjectId) {
        List<Integer> bzHouseIdList = new ArrayList<>();
        List<Integer> pxHouseIdList = new ArrayList<>();
        List<Integer> houseIdList = new ArrayList<>();
        ApplicationContext applicationContext = ApplicationContextProvider.getApplicationContext();
        houseService = (HouseService) applicationContext.getBean(HouseService.class);
        String houseHoldStr = "";
        Integer limitType = contract.getChooseLimitType();
        List<House> chooseHouseList = houseService.getHouseListByContractIdForBuildArea(contract.getId());
        List<House> bzChooseHouseList = houseService.getHouseListByContractIdForBuildAreaAndBz(contract.getId(), "保障区房源");
        List<House> jyChooseHouseList = houseService.getHouseListByContractIdForBuildAreaAndBz(contract.getId(), "结余区房源");
        List<House> noBzChooseHouseList = houseService.getHouseListByContractIdForBuildAreaAndNoBz(contract.getId(), "'普选区房源','保障转普选','期房区房源'");
        Map<String, Integer> houseHoldCountMap = houseService.getHouseMapByHouseHoldAndZsprojectId(contract.getZsProject().getId());
        //已选保障房最小面积
        BigDecimal minBzArea = null;
        if (bzChooseHouseList.size() > 0) {
            //获取面积最小的house
            House minBzAreaHouse = bzChooseHouseList.stream().min(Comparator.comparing(House::getBuildArea)).get();
            //获取已选房非保障房最小面积
            minBzArea = minBzAreaHouse.getBuildArea();
        } else {
            minBzArea = new BigDecimal("0");
        }
        BigDecimal minNoBzArea = null;
        String minNoBzHouseHold = null;
        if (noBzChooseHouseList.size() > 0) {
            //获取面积最小的house
            House minNoBzAreaHouse = noBzChooseHouseList.stream().min(Comparator.comparing(House::getBuildArea)).get();
            //获取已选房非保障房最小面积
            minNoBzArea = minNoBzAreaHouse.getBuildArea();
            minNoBzHouseHold = minNoBzAreaHouse.getHouseHold();
        } else {
            minNoBzArea = new BigDecimal("0");
            minNoBzHouseHold = "";
        }
        if (limitType != null && limitType == 1) {
            houseIdList = null;
            return houseIdList;
        } else if (limitType != null && limitType == 2) {
            houseIdList = null;
            return houseIdList;
        } else if (limitType != null && limitType == 3) {

            BigDecimal allChooseArea = BigDecimal.ZERO;
            BigDecimal syAllChooseArea = BigDecimal.ZERO;
            for (House house : chooseHouseList) {
                //当前所有已选房面积合计
                allChooseArea = BigDecimalUtils.getSum(allChooseArea, house.getBuildArea());
            }
            //剩余可选房面积
            syAllChooseArea = BigDecimalUtils.getBigAndBigSub(contract.getLimitHouseAreaTotal(), allChooseArea);
            if (syAllChooseArea.compareTo(BigDecimal.ZERO) < 0) {
                syAllChooseArea = BigDecimal.ZERO;
            }


            //如果选择过非保障房，则不可再选择保障房，必须先选择保障房
            if (noBzChooseHouseList.size() == 0 && jyChooseHouseList.size() == 0) {
                //判断该户是否选择保障房，且三房、四房分别选了多少
                int bzChooseNum0302 = 0;
                int bzChooseNum0402 = 0;
                if (CollectionUtils.isNotEmpty(bzChooseHouseList)) {
                    for (House house : bzChooseHouseList) {
                        if (house.getHouseHold().equals("0402")) {
                            bzChooseNum0402 = bzChooseNum0402 + 1;
                        } else if (house.getHouseHold().equals("0302")) {
                            bzChooseNum0302 = bzChooseNum0302 + 1;
                        }
                    }
                }

                BigDecimal bzMaxArea = syAllChooseArea;

                //判断并且循环安排所需户型，并根据实际已选情况判断剩余可选保障房情况户型套数
                if (StringUtils.isNotEmpty(contract.getBaoZhangType())) {
                    List<String> baoZhangTypeList = Arrays.asList(contract.getBaoZhangType().split(";"));
                    for (String s : baoZhangTypeList) {
                        if (s.contains("可保障三房二厅")) {
                            if (s.contains("1")) {
                                if (bzChooseNum0302 == 0) {
                                    houseHoldStr = houseHoldStr + "0302,";
                                    //该情况下该户可选择保障房三房二厅一套
                                } else if (bzChooseNum0302 == 1) {
                                    //该情况下该户已选择保障房三房二厅一套，不可再选三房二厅保障房
                                }
                            } else if (s.contains("2")) {
                                if (bzChooseNum0302 == 0) {
                                    houseHoldStr = houseHoldStr + "0302,";
                                    //该情况下该户可选择保障房三房二厅二套
                                } else if (bzChooseNum0302 == 1) {
                                    houseHoldStr = houseHoldStr + "0302,";
                                    //该情况下该户已选择保障房三房二厅一套，可再选择保障房三房二厅一套
                                } else if (bzChooseNum0302 == 2) {
                                    //该情况下该户已选择保障房三房二厅二套，不可再选三房二厅保障房
                                }
                            }
                        } else if (s.contains("可保障四房二厅")) {
                            if (s.contains("1")) {
                                if (bzChooseNum0402 == 0) {
                                    //该情况下该户可选择保障房四房二厅一套
                                    houseHoldStr = houseHoldStr + "0402";
                                } else if (bzChooseNum0402 == 1) {
                                    //该情况下该户已选择保障房四房二厅一套，不可再选四房保障房
                                }
                            } else if (s.contains("2")) {
                                if (bzChooseNum0402 == 0) {
                                    houseHoldStr = houseHoldStr + "0402";

                                    //该情况下该户可选择保障房四房二厅二套
                                } else if (bzChooseNum0402 == 1) {
                                    houseHoldStr = houseHoldStr + "0402";

                                    //该情况下该户已选择保障房四房二厅一套，可再选择保障房四房二厅一套
                                } else if (bzChooseNum0402 == 2) {
                                    //该情况下该户已选择保障房四房二厅二套，不可再选四房保障房
                                }
                            } else if (s.contains("3")) {
                                if (bzChooseNum0402 == 0) {
                                    houseHoldStr = houseHoldStr + "0402";
                                    //该情况下该户可选择保障房四房二厅三套
                                } else if (bzChooseNum0402 == 1) {
                                    houseHoldStr = houseHoldStr + "0402";
                                    //该情况下该户已选择保障房四房二厅一套，可再选择保障房四房二厅二套
                                } else if (bzChooseNum0402 == 2) {
                                    houseHoldStr = houseHoldStr + "0402";
                                    //该情况下该户已选择保障房四房二厅二套，可再选择保障房四房二厅一套
                                } else if (bzChooseNum0402 == 3) {
                                    //该情况下该户已选择保障房四房二厅三套，不可再选四房保障房
                                }
                            }
                        }
                        if (StringUtils.isNotEmpty(houseHoldStr) && houseHoldStr.contains("0402")) {
                            List<BigDecimal> bzBuildAreaIntList = houseService.getBzBuildAreaIntListByHouseId(contract.getZsProject().getId(), azProjectId, syAllChooseArea);
                            int bzNum = 1;
                            //最大可选面积
                            if (bzBuildAreaIntList.size() > 0) {
                                for (BigDecimal bigDecimal : bzBuildAreaIntList) {
                                    if (bigDecimal.compareTo(bzMaxArea) > 0) {
                                        bzMaxArea = BigDecimalUtils.getSum(bigDecimal, new BigDecimal("0.99"));
                                    } else {
                                        bzMaxArea = BigDecimal.ZERO;
                                    }
                                    //二选一最大可选套型
                                    //区间二选一
                                    if (bzNum == 2) {
                                        break;
                                    }
                                    bzNum = bzNum + 1;
                                }
                            }
                        }
                    }
                }
                //可选房源不可超过当前所选最小房面积
                if (minBzArea.compareTo(BigDecimal.ZERO) > 0 && bzMaxArea.compareTo(minBzArea) > 0) {
                    bzMaxArea = minBzArea;
                }
                bzHouseIdList = houseService.getBzHouseIdListByZsProjectIdAndHouseStyleAndArea(contract.getZsProject().getId(), bzMaxArea, houseHoldStr);
            } else {
                bzHouseIdList = houseService.getBzHouseIdListByZsProjectIdAndHouseStyleAndArea(contract.getZsProject().getId(), BigDecimal.ZERO, houseHoldStr);
            }


            //非保障房选房不可选房源（普选区、期房区、保障转普选）
            houseHoldStr = "";

            //如果已选房面积大于总安置面积
            if (syAllChooseArea.compareTo(new BigDecimal("40")) < 0) {
                //小于40不允许选房
                pxHouseIdList = houseService.getPxHouseIdListByZsProjectIdAndHouseStyleAndArea(contract.getZsProject().getId(), BigDecimal.ZERO, houseHoldStr);
            } else {
                houseHoldStr = getHouseHoldStr(syAllChooseArea, houseHoldCountMap);

                //比剩余安置面积大的套型面积
                List<BigDecimal> buildAreaIntList = houseService.getBuildAreaIntListByHouseIdAndChooseLimitMemo(contract.getZsProject().getId(), azProjectId, houseHoldStr, syAllChooseArea);
                int num = 1;
                BigDecimal pxMaxArea = syAllChooseArea;
                for (BigDecimal bigDecimal : buildAreaIntList) {
                    if (bigDecimal.compareTo(pxMaxArea) > 0) {
                        pxMaxArea = BigDecimalUtils.getSum(bigDecimal, new BigDecimal("0.99"));
                    }
                    //二选一最大可选套型
                    //区间二选一
                    if (num == 2) {
                        break;
                    }
                    num = num + 1;
                }
                //可选房源不可超过当前所选最小房面积
                if (minNoBzArea.compareTo(BigDecimal.ZERO) > 0 && pxMaxArea.compareTo(minNoBzArea) > 0) {
                    pxMaxArea = minNoBzArea;
                    houseHoldStr = minNoBzHouseHold;
                }
                pxHouseIdList = houseService.getPxHouseIdListByZsProjectIdAndHouseStyleAndArea(contract.getZsProject().getId(), pxMaxArea, houseHoldStr);

            }
        }
        houseIdList.addAll(pxHouseIdList);
        houseIdList.addAll(bzHouseIdList);
        return houseIdList;
    }

    public static String getHouseHoldStr(BigDecimal area, Map<String, Integer> houseHoldCountMap) {
        String houseHoldStr = "";
        if (area.compareTo(BigDecimal.ZERO) >= 0
                && area.compareTo(new BigDecimal("40")) < 0) {
            //如果面积小于40，则不允许选房
            houseHoldStr = "";
        } else if (area.compareTo(new BigDecimal("40")) >= 0
                && area.compareTo(new BigDecimal("90")) < 0) {
            //如果面积大于等于40，小于90，则,最大允许选择二房二厅
            houseHoldStr = "0202";
            if (houseHoldCountMap.get("0202") == null || houseHoldCountMap.get("0202") == 0) {
                //如果二房二厅房源没了，则,最大允许选择三房二厅
                houseHoldStr = "0302";
                if ((houseHoldCountMap.get("0302") == null || houseHoldCountMap.get("0302") == 0)) {
                    //如果三房二厅房源，复式房源没了，则,最大允许选择四房二厅
                    houseHoldStr = "0402";
                }
            }
        } else if (area.compareTo(new BigDecimal("90")) >= 0
                && area.compareTo(new BigDecimal("130")) < 0) {
            houseHoldStr = "0302";
            //如果面积大于等于90，小于130，则,允许选择二房二厅、三房二厅,五房二厅,复式
            if ((houseHoldCountMap.get("0302") == null || houseHoldCountMap.get("0302") == 0)) {
                //如果二房二厅、三房二厅,五房二厅,复式 房源没了，则,允许向上选择四房二厅
                houseHoldStr = "0402";
            }
        } else if (area.compareTo(new BigDecimal("130")) >= 0) {
            //如果面积大于等于130，则,允许选择所有户型
            houseHoldStr = "0402";
        }
        return houseHoldStr;
    }

    /**
     * 获取某户当前不可选房源IdList
     *
     * @param contract
     * @param chooseHouseList 预选房源List<House>
     * @param chooseHouseArea 预选房源总面积
     * @return
     */
    public static List<Integer> getCountChooseHouseIdListForApp(Contract contract, List<House> chooseHouseList, BigDecimal chooseHouseArea) {
        List<Integer> houseIdList = new ArrayList<>();
        ApplicationContext applicationContext = ApplicationContextProvider.getApplicationContext();
        houseService = (HouseService) applicationContext.getBean(HouseService.class);
        String chooseLimitMemo = null;
        Integer limitType = contract.getChooseLimitType();
        //收项目所分配房源的每个户型所对应的房源，并封装map
        Map<String, Integer> houseHoldCountMap = houseService.getHouseMapByHouseHoldAndZsprojectId(contract.getZsProject().getId());
        BigDecimal minArea = null;
        if (chooseHouseList.size() > 0) {
            //获取面积最小的house
            House minAreaHouse = chooseHouseList.stream().min(Comparator.comparing(House::getBuildArea)).get();
            //获取已选房最小面积
            minArea = minAreaHouse.getBuildArea();
        } else {
            minArea = new BigDecimal("0");
        }
        if (limitType != null && limitType == 1) {
            houseIdList = null;
        } else if (limitType != null && limitType == 2) {
            houseIdList = null;
        } else if (limitType != null && limitType == 3) {
            //判断是否有选过保障区房源
            for (House house : chooseHouseList) {
                if (house.getHouseStyle() != null && house.getHouseStyle().equals("保障区房源")) {
                    chooseLimitMemo = "已选择保障区房源";
                }
            }
            //判断是否有选房，如果未选房，则不大于总安置面积，且升档三个套型
            if (chooseHouseList.size() == 0) {
                String houseHoldStr = "";
                //如果面积大于0 小于 40 则不允许选房
                if (contract.getLimitHouseAreaTotal().compareTo(BigDecimal.ZERO) > 0
                        && contract.getLimitHouseAreaTotal().compareTo(new BigDecimal("40")) < 0) {
                    return null;
                } else if (contract.getLimitHouseAreaTotal().compareTo(new BigDecimal("40")) >= 0
                        && contract.getLimitHouseAreaTotal().compareTo(new BigDecimal("90")) < 0) {
                    //如果面积大于等于40，小于90，则,允许选择二房二厅
                    houseHoldStr = "'0202'";
                    if (houseHoldCountMap.get("0202") == null || houseHoldCountMap.get("0202") == 0) {
                        //如果二房二厅房源没了，则,允许选择三房二厅
                        houseHoldStr = "'0302','1000'";
                        if ((houseHoldCountMap.get("0302") == null || houseHoldCountMap.get("0302") == 0)
                                && (houseHoldCountMap.get("1000") == null || houseHoldCountMap.get("1000") == 0)) {
                            //如果三房二厅房源，复式房源没了，则,允许选择四房二厅，五房二厅
                            houseHoldStr = "'0402','0502'";
                        }
                    }
                } else if (contract.getLimitHouseAreaTotal().compareTo(new BigDecimal("90")) >= 0
                        && contract.getLimitHouseAreaTotal().compareTo(new BigDecimal("125")) < 0) {
                    houseHoldStr = "'0202','0302','1000'";
                    //如果面积大于等于90，小于125，则,允许选择二房二厅、三房二厅
                    if ((houseHoldCountMap.get("0302") == null || houseHoldCountMap.get("0302") == 0)
                            && (houseHoldCountMap.get("1000") == null || houseHoldCountMap.get("1000") == 0)) {
                        //如果三房二厅、复式房源没了，则,允许向上选择四房二厅，五房二厅，
                        houseHoldStr = "'0202','0402','0502'";

                    }
                } else if (contract.getLimitHouseAreaTotal().compareTo(new BigDecimal("125")) >= 0) {
                    //如果面积大于等于125，则,允许选择所有户型
                    houseHoldStr = "'0202','0302','0402','0502','1000'";
                }
                List<BigDecimal> buildAreaIntList = houseService.getBuildAreaIntListByHouseId(contract, houseHoldStr, contract.getLimitHouseAreaTotal());
                int num = 1;
                //最大可选面积
                BigDecimal maxArea = contract.getLimitHouseAreaTotal();
                for (BigDecimal bigDecimal : buildAreaIntList) {
                    if (bigDecimal.compareTo(maxArea) > 0) {
                        maxArea = BigDecimalUtils.getSum(bigDecimal, new BigDecimal("0.99"));
                    }
                    //二选一最大可选套型
                    //区间二选一
                    if (contract.getChooseHouseNum().contains("-") && num == 2) {
                        break;
                    } else if (!contract.getChooseHouseNum().contains("-") && num == 3) {
                        //三选一最大可选套型
                        break;
                    }
                    num = num + 1;
                }
                //如果第一套房，最大可选面积为总可安置面积，
                houseIdList = houseService.getHouseIdListByZsProjectIdAndHouseStyle(contract.getZsProject().getId(), chooseLimitMemo, maxArea);
            } else {
                //不是第一套
                //如果已选房面积大于总安置面积
                if (chooseHouseArea.compareTo(contract.getLimitHouseAreaTotal()) >= 0) {
                    //已扩购则不允许选房
                    houseIdList = houseService.getHouseIdListByZsProjectIdAndHouseStyle(contract.getZsProject().getId(), chooseLimitMemo, new BigDecimal("0"));
                } else {
                    BigDecimal syAllChooseArea = BigDecimal.ZERO;
                    //剩余可选房面积
                    syAllChooseArea = BigDecimalUtils.getBigAndBigSub(contract.getLimitHouseAreaTotal(), chooseHouseArea);

                    String houseHoldStr = "";
                    if (syAllChooseArea.compareTo(BigDecimal.ZERO) > 0
                            && syAllChooseArea.compareTo(new BigDecimal("40")) < 0) {
                        //如果面积小于40，则不允许选房
                        return null;
                    } else if (syAllChooseArea.compareTo(new BigDecimal("40")) >= 0
                            && syAllChooseArea.compareTo(new BigDecimal("90")) < 0) {
                        //如果面积大于等于40，小于90，则,允许选择二房二厅
                        houseHoldStr = "'0202'";
                        if (houseHoldCountMap.get("0202") == null || houseHoldCountMap.get("0202") == 0) {
                            //如果二房二厅房源没了，则,允许选择三房二厅
                            houseHoldStr = "'0302','1000'";
                            if ((houseHoldCountMap.get("0302") == null || houseHoldCountMap.get("0302") == 0)
                                    && (houseHoldCountMap.get("1000") == null || houseHoldCountMap.get("1000") == 0)) {
                                //如果三房二厅房源没了，则,允许选择四房二厅，五房二厅，复式房源
                                houseHoldStr = "'0402','0502'";
                            }
                        }
                    } else if (syAllChooseArea.compareTo(new BigDecimal("90")) >= 0
                            && syAllChooseArea.compareTo(new BigDecimal("125")) < 0) {
                        houseHoldStr = "'0202','0302','1000'";
                        //如果面积大于等于90，小于125，则,允许选择二房二厅、三房二厅
                        if ((houseHoldCountMap.get("0302") == null || houseHoldCountMap.get("0302") == 0)
                                && (houseHoldCountMap.get("1000") == null || houseHoldCountMap.get("1000") == 0)) {
                            //如果三房二厅房源没了，则,允许向上选择四房二厅，五房二厅，复式房源
                            houseHoldStr = "'0202','0402','0502'";

                        }
                    } else if (syAllChooseArea.compareTo(new BigDecimal("125")) >= 0) {
                        //如果面积大于等于125，则,允许选择所有户型
                        houseHoldStr = "'0202','0302','0402','0502','1000'";
                    }

                    //比剩余安置面积大的套型面积
                    List<BigDecimal> buildAreaIntList = houseService.getBuildAreaIntListByHouseId(contract, houseHoldStr, syAllChooseArea);
                    int num = 1;
                    BigDecimal maxArea = syAllChooseArea;
                    for (BigDecimal bigDecimal : buildAreaIntList) {
                        if (bigDecimal.compareTo(maxArea) > 0) {
                            maxArea = BigDecimalUtils.getSum(bigDecimal, new BigDecimal("0.99"));
                        }
                        //二选一最大可选套型
                        //区间二选一
                        if (num == 2) {
                            break;
                        }
                        num = num + 1;
                    }
                    //可选房源不可超过当前所选最小房面积
                    if (maxArea.compareTo(minArea) > 0) {
                        maxArea = minArea;
                    }
                    houseIdList = houseService.getHouseIdListByZsProjectIdAndHouseStyle(contract.getZsProject().getId(), chooseLimitMemo, maxArea);

                }
            }
        }
        return houseIdList;
    }

}